import datetime
import os
import random
from random import randint
import time
from faker import Faker


faker = Faker(locale='en_US')
user_sql = "INSERT into USER values('{w_id}','{name}','!QAZ2wsx','{tel}')"
detail_sql = "INSERT INTO DETAIL VALUES('{w_id}',{sex},{age},'{p}','{time}')"
server_sql = "INSERT INTO SERVER VALUES({id},'{hostname}','{os}','{mem}','{cpu}','{ip}','{oper}','{pwd}','{vs}','{fun}')"

def name_list():
    name = []
    for i in range(20):
        lr = chr(random.randint(65,90))
        le = ''.join(random.choice(chr(random.randint(97,122))) for i in range(5))
        name.append(lr + ''.join(le))
    return name


def work_list():
    w_id = []
    for i in range(192001, 192021):
        w_id.append(''.join(str(i)))
    return w_id


def tel_list():
    tel = '176' + ''.join(str(random.choice(range(10))) for _ in range(8))
    return tel


def name_work(name, work):
    dict_name = {}
    for i in range(len(name)):
        dict_name[work[i]] = name[i]
    return dict_name


def post(work):
    dict_post = {}
    p = ['Java开发', 'Python开发', 'PHP开发', '系统运维工程师', '运维工程师', '助理', 'HR', '测试工程师']
    for i in range(len(work)):
        dict_post[work[i]] = random.choice(p)
    return dict_post

# # user表数据
# data = name_work(name=name_list(), work=work_list())
# with open('user.sql', 'w', encoding='utf-8') as file:

#     for x in list(data.keys()):
#         file.write(user_sql.format(w_id=x,name=data[x],tel=tel_list()))
#         file.write(';' + '\n')


# # details表数据
# data = post(work=work_list())
# with open('detail.sql', 'w', encoding='utf-8') as file:
#     for x in list(data.keys()):
#         file.write(detail_sql.format(w_id=x,sex=random.choice([0,1]),age=int(random.choice(range(20,40))),p=data[x],time=time.strftime("%Y-%m-%d %H:%M:%S")))
#         file.write(';' + '\n')


# # server表数据
# with open('server.sql', 'w', encoding='utf-8') as file:
#     for i in range(1,100):
#         file.write(server_sql.format(id=i,
#         hostname=faker.pystr(),
#         os=random.choice(['centos7','redhat7','suse12','ubandu20','centos8','redhat8','suse13','suse14']),
#         mem=str(random.choice([2,4,8,16])) + ''.join('G'),
#         ip=faker.ipv4(),
#         cpu=random.choice([1,2,3,4,5,6,7,8]),
#         oper=random.choice(['pay_app','otms_app', 'cets_app','root']),
#         pwd=faker.password(),
#         vs='500G',
#         fun='服务器'))
#         file.write(';' + '\n')